package com.baosight.sczx.zm.cg.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.StaticBlocks;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.cs.WorkSchedule;
import com.baosight.sczx.zm.cg.domain.ZMCGDutyschedule;

/**
 * 调度排班管理
 * <p>Title: ServiceZMCG50.java</p> 
 */
public class ServiceZMCG50 extends ServiceEPBase {

	/**
	 * 初始化方法 
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}

	/**
	 * 查询方法 
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		String fdMiningId = UserSession.getInSessionProperty("miningCode").toString();
		StaticTools.setQueryParameters(inInfo, "fdMiningId", fdMiningId, true);
		String date = inInfo.getString("querydate");
		if(date != null && !date.trim().equals("")) {
			StaticTools.setQueryParameters(inInfo, "fdVirtualDate", date, true);
		}
		EiInfo outInfo = super.query(inInfo, "ZMCGDutyschedule.query", new ZMCGDutyschedule());
		outInfo.addBlock(WorkSchedule.getWorkScheduleBlock("cmes.cg.workNum", fdMiningId, true));//班次
		outInfo.addBlock(StaticBlocks.getBlockData("cmes.cg.status", new String[]{"草拟", "生效"}, new String[]{"草拟", "生效"}, true));//状态
	
		return outInfo;
	}
	
	/**
	 * <p>Function: insert</p> 
	 */
	@Override	
	public EiInfo insert(EiInfo inInfo) {
		EiInfo out = new EiInfo(); 
		ZMCGDutyschedule _dom = new ZMCGDutyschedule();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		Map<String, String> params = new HashMap<String, String>();
		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));

			_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CG_DUTYSCHEDULE"));
			_dom.setFdCreateDepartmentId(UserSession.getInSessionProperty("orgCode").toString());
			_dom.setFdCreateDepartment(UserSession.getInSessionProperty("orgName").toString());
			_dom.setFdMiningId(UserSession.getInSessionProperty("miningCode").toString());
			_dom.setFdMiningName(UserSession.getInSessionProperty("miningName").toString());
			_dom.setFdCreateUserId(UserSession.getUserId().toString());
			_dom.setFdCreateUser(UserSession.getInSessionProperty("UserCname").toString());
			_dom.setFdVirtualDate(WorkSchedule.getVirtualDate(_dom.getFdEditDate(),_dom.getFdWorknumId()));
			
			boolean validate = this.validate(inInfo, _dom, params);
			if(validate){
				out = this.query(inInfo);
				return out;
			}
			this.dao.insert("ZMCGDutyschedule.insert", _dom);
		}
		
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out; 
	}
	
	/**
	 * <p>Function: update</p> 
	 */
	@Override	
	public EiInfo update(EiInfo inInfo) {
		EiInfo out = new EiInfo(); 
		ZMCGDutyschedule _dom = new ZMCGDutyschedule();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		
		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));

			_dom.setFdCreateUserId(UserSession.getUserId().toString());
			_dom.setFdCreateUser(UserSession.getInSessionProperty("UserCname").toString());
			_dom.setFdVirtualDate(WorkSchedule.getVirtualDate(_dom.getFdEditDate(),_dom.getFdWorknumId()));
			
			Map<String, String> params = new HashMap<String, String>();
			params.put("fdKeyId", _dom.getFdKeyId());
			boolean validate = this.validate(inInfo, _dom, params);
			if(validate){
				out = this.query(inInfo);
				return out;
			}
			this.dao.insert("ZMCGDutyschedule.update", _dom);
		}
		
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;
	}
	
	/**
	 * <p>Function: delete</p> 
	 */
	@Override	
	public EiInfo delete(EiInfo inInfo) { 
		EiInfo _temp = super.delete(inInfo, "ZMCGDutyschedule.delete");
		EiInfo out = this.query(inInfo);
		out.setMsg(_temp.getMsg());
		out.setDetailMsg(_temp.getDetailMsg());
		return out; 
	}
	
	/***
	 * 生效
	 * @param inInfo
	 * @return
	 */
	public EiInfo report(EiInfo inInfo) { 
		EiInfo _temp = super.update(inInfo, "ZMCGDutyschedule.report");
		EiInfo out = this.query(inInfo);
		out.setMsg(_temp.getMsg());
		out.setDetailMsg(_temp.getDetailMsg());
		return out; 
	}
	
	/**
	 * 校验是否重复
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-16下午12:48:34 张志芳
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private boolean validate(EiInfo inInfo, ZMCGDutyschedule _dom, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		
		params.put("fdWorknumId", _dom.getFdWorknumId());
		params.put("fdMiningId", _dom.getFdMiningId());
		params.put("fdEditDate", _dom.getFdEditDate());
		
		List result = dao.query("ZMCGDutyschedule.validate", params);
		if(result != null && result.size() > 0) {
			inInfo.setStatus(-1);
			String msg = "每天每班只有存在一条记录！";
			inInfo.setMsg(msg);
			return true;
		}
		return false;
	}
}
